package com.yuzhitong.shapi.base.recycler;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

/**
 * 左侧物流时间线
 */
public class TimeLineItemDecoration extends RecyclerView.ItemDecoration {

    private Paint mPaint;//画笔
    private int mCircleSize = 14;//圆圈的半径
    private int mPaintSize = 6;//画笔宽度
    private String mPaintColor = "#F73D3D";//画笔默认颜色

    public TimeLineItemDecoration() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(mPaintSize);
        mPaint.setColor(Color.parseColor(mPaintColor));
    }

    @Override
    public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        Rect rect = new Rect(200, 0, 0, 0);//使item从outRect中右移200px
        outRect.set(rect);
    }

    /**
     * 设置圆的半径
     *
     * @param mCircleSize 半径大小，单位px
     */
    public void setCircleSize(int mCircleSize) {
        this.mCircleSize = mCircleSize;
    }

    /**
     * 设置画笔宽度
     *
     * @param mPaintSize 画笔宽度，单位px
     */
    public void setPaintSize(int mPaintSize) {
        this.mPaintSize = mPaintSize;
    }

    /**
     * 设置画笔颜色
     *
     * @param mPaintColor 画笔颜色
     */
    public void setPaintColor(String mPaintColor) {
        this.mPaintColor = mPaintColor;
    }

    @Override
    public void onDraw(@NotNull Canvas canvas, @NotNull RecyclerView parent, @NotNull RecyclerView.State state) {
        super.onDraw(canvas, parent, state);

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            int left = child.getLeft();
            int top = child.getTop();
            int bottom = child.getBottom();

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int bottomMargin = params.bottomMargin;//防止在item根布局中设置marginTop和marginBottom
            int topMargin = params.topMargin;

            int leftX = left / 2;//轴线的x轴坐标
            int height = child.getHeight();//item的高度，不包含Margin

            int heightHalf = height / 2;

            if (childCount == 1) {
                canvas.drawLine(leftX, top, leftX, bottom - heightHalf, mPaint);
            } else {
                if (i == 0) {
                    canvas.drawLine(leftX, top + heightHalf, leftX, bottom + bottomMargin, mPaint);
                } else if (i == childCount - 1) {
                    canvas.drawLine(leftX, top - topMargin, leftX, bottom - heightHalf, mPaint);
                } else {
                    canvas.drawLine(leftX, top - topMargin, leftX, bottom - heightHalf, mPaint);
                    canvas.drawLine(leftX, top + heightHalf, leftX, bottom + bottomMargin, mPaint);
                }
            }
            canvas.drawCircle(leftX, top + heightHalf, mCircleSize, mPaint);
        }
    }
}